<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Action
 *
 * @author minhhq
 */
class MKT_Controller_Action extends Zend_Controller_Action {
    /**
     *
     * @var MKT_Facebook
     */
    protected $_facebook;
    
    /**
     *
     * @var Core_Model_User
     */
    protected $_user;
    
    public function init() {
        parent::init();
        $this->_helper->layout->setLayout("layout");
        $this->_facebook = MKT_Facebook::getInstance();
        $translate = Zend_Registry::get("translate");
        $this->view->translate = $translate;
        $this->view->facebook = $this->_facebook;
        
        $this->_authorize();
    }
    
    protected function setAjaxCall() {
        $this->_helper->layout()->disableLayout();
        $this->_helpet->viewRenderer->setNoRender(true);
    }
    
    protected function _authorize() {
        $id_social_user = $this->_facebook->getUser();
        $user_db = new Core_Model_DbTable_Users();
        if (!empty($id_social_user)) {
            $user = $user_db->fetchRow("id_social_user = '$id_social_user'");
            if ($user) {
                $this->_user = $user;
                if ($this->_facebook->hasNewSignal()) {
                    $this->_updateUser();
                }
            } else {
                $this->_user = $user_db->createRow();
                $this->_updateUser();
            }
        }
    }
    
    private function _updateUser() {
        $user_profile = $this->_facebook->api("/me");
        $user_data = array();
        
        $user_data['id_social_user'] = $user_profile['id'];
        $user_data['first_name']     = isset($user_profile['first_name']) ? $user_profile['first_name'] : NULL;
        $user_data['last_name']      = isset($user_profile['last_name'])  ? $user_profile['last_name'] : NULL;
        $user_data['username']       = isset($user_profile['username'])   ? $user_profile['username'] : NULL;
        $user_data['name']           = isset($user_profile['name'])       ? $user_profile['name'] : NULL;
        $user_data['gender']         = isset($user_profile['gender'])     ? $user_profile['gender'] : NULL;
        $user_data['locale']         = isset($user_profile['locale'])     ? $user_profile['locale'] : NULL;
        $user_data['timezone']       = isset($user_profile['timezone'])   ? $user_profile['timezone'] : NULL;
        $user_data['birthday']       = isset($user_profile['birthday'])   ? $user_profile['birthday'] : NULL;
        $user_data['email']          = isset($user_profile['email'])      ? $user_profile['email'] : NULL;
        
        if (isset($user_profile['hometown'])) {
            $user_data['hometown'] = $user_profile['hometown']['name'];
        }
        
         if (isset($user_profile['location'])) {
            $user_data['location'] = $user_profile['location']['name'];
        }
        
        $user_db = new Core_Model_DbTable_Users();
        $user_cols = $user_db->info("cols");
        
        $need_to_update = false;
        foreach ($user_data as $key => $value) {
            if (in_array($key, $user_cols) && !empty($value) && ($value != $this->_user->$key)) {
                $need_to_update = true;
                break;
            }
        }
        
        if ($need_to_update) {
            $user_data['updated_time'] = time();
            $this->_user->setFromArray($user_data);
            $this->_user->save();
        }
        
        $this->_handleUserAccessTooken($this->_user);
    }
    
        
    /**
     * 
     * @param YouNet_Model_User
     * @return boolean
     */
    private function _handleUserAccessTooken($user) {
        return $this->_facebook->handleUserAccessTooken($user);
    }
}

?>
